<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use app\admin\controller\Base;
class Power extends Base
{
    public function privilege()
    {
        $info = Db::query("select * from user where user_status='1'  ");
       // var_dump($info);die;
        return $this->fetch('privilege',['data'=>$info]);
    }

    public function addprivilege()
    {
        $info = Db::query("select * from role where role_isshow='0'  ");
        return $this->fetch('addprivilege',['data'=>$info]);

    }
    public function delprivilege(){
        $id=input('get.id');
        $info = Db::query("update user set user_status='0' where user_id=$id  ");
        $this->success('删除成功','admin/Power/privilege');
             
    }
    public function tianprivilege(){
        $user_name=input('post.user_name');
        $user_password=input('post.password');
        $user_email=input('post.email');
        $user_repassword=input('post.pwd_confirm');
        $time=time();
        if($user_password!=$user_repassword){
             $this->error('两次密码不一样，请重新输入','admin/Power/addprivilege');die;
        }
        $sql="insert into user (user_name,user_password,user_email,user_intime,user_lastlogintime) values ('$user_name','$user_password','$user_email','$time','$time')";
        Db::execute($sql);
        $userid = Db::name('user')->getLastInsID();
        $roleid=input('post.rolename');
        $sql="insert into user_role (user_id,role_id) values ('$userid','$roleid')";
        Db::execute($sql);
        $this->success('添加成功','admin/Power/privilege');
    }



    public function role()
    {   
        $info = Db::query("select * from role where role_isshow='0'  ");
        return $this->fetch('role',['data'=>$info]);
    }

    public function addrole()
    {   
        header("content-type:text/html; charset=utf-8");
        $info = Db::query("select * from power where power_status='0' ");
        //echo "<pre>";
        //var_dump($info);
        $arr=array();
        foreach ($info as $key => $val) {
            foreach ($val as $k => $v) {
                if($k=='parent_id' && $v=='0'){
                    $arr[]=$info[$key];
                }
            }
        }
        $conarr=array();
        foreach ($arr as $key => $value) {
            foreach ($value as $k => $v) {
                if($k=='power_controllername'){
                    $conarr[]=$v;
                }
            }
        }
        $actarr=array();
        foreach ($conarr as $conarrkkey => $conarrval) {
            foreach ($info as $key => $val) {
                foreach ($val as $k => $v) {
                    if($k=='power_controllername' && $v==$conarrval){
                        $actarr[$conarrval][]=$info[$key];
                    }
                }
            }
        }

        return $this->fetch('addrole',['data'=>$actarr]);

    }
    public function tianrole(){
        header("content-type:text/html; charset=utf-8");
        $role_name=input('post.user_name');
        $role_desc=input('post.role_describe');
        $data=input('post.');
        $role_code=array();
        $role_code=$data['action_code'];
        $role_addtime=time();
        $sql="insert into role (role_name,role_desc,role_addtime) values ('$role_name','$role_desc','$role_addtime')";
        $info = Db::execute($sql);
        $roleid = Db::name('role')->getLastInsID();
        foreach ($role_code as $key => $value) {
            $rolepowersql="insert into role_power (role_id,power_id) values ('$roleid','$value')";
            $res = Db::execute($rolepowersql);
        }
        $this->success('添加成功','admin/Power/role');
    }

    public function delrole(){
        $id=input('get.roleid');
        $info = Db::execute("update role set role_isshow='1' where role_id=$id  ");
        $this->success('删除成功','admin/Power/role');
             
    }

}